<?php

namespace Biz\module\Classes\adm;

use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderRefund as mOrderRefund;


class OrderRightList
{
    public function getInfos($refundOrderNos)
    {
        $list = [];
        if ($refundOrderNos) {
            $list = mOrderRefund::select('*', ['refund_order_no' => $refundOrderNos, 'ORDER' => ['addtime' => 'DESC']]);
            $orderNoList = array_column($list, 'order_no');
            $orderList = mOrder::getOrderInfoList(['order_no' => $orderNoList], [], [], true);

            $orderList = array_column($orderList, null, 'order_no');

            if ($list) {
                $nowTime = time();
                foreach ($list as &$info) {
                    mOrderRefund::formatInfo($info, 'adm');

                    $info['order_status'] = $orderList[$info['order_no']]['status'];
                    $info['order_status_desc'] = $orderList[$info['order_no']]['status_desc'];

                    $info['timeout_desc'] = '';

                    if ($info['rights_status'] == mOrderRefund::RIGHTS_STATUS_NORMAL) {
                        if (strtotime($info['rights_into_timeout_time']) < $nowTime) {
                            $info['timeout_desc'] = '超时';
                        }
                    }

                    if ($info['rights_status'] == mOrderRefund::RIGHTS_STATUS_PROOFING) {
                        if ((strtotime($info['rights_into_time']) + mOrderRefund::RIGHT_JUDGE_TIME_OUT) < $nowTime) {
                            $info['timeout_desc'] = '超时';
                        }
                    }

                    $info['order_goods'] = [
                        'sku_desc' => $info['sku_desc'],
                        'title' => $info['title'],
                        'cover_img' => $info['cover_img_thumb_url'],
                        'cover_img_big' => $info['cover_img_url']
                    ];


                }
            }
        }
        return $list;
    }

    /**
     * @param $list
     * @param $sortType
     */
    public static function sellerRightList($list, $sortType)
    {
        $refundNos = $list['rows'];
        $sort = [
            1 => ['addtime' => 'DESC'],
            2 => ['addtime' => 'ASC'],
        ];
        $orderRefunds = mOrderRefund::select('*', ['AND' => ['refund_order_no' => $refundNos], 'ORDER' => $sort[$sortType]]);
        if ($refundNos) {
            array_walk($orderRefunds, function (&$v, $k) use (&$orderRefunds, $list) {

                mOrderRefund::formatInfo($v, 'mgt');

                $orderRefunds[$k]['sku_info'] = [
                    'sku_id' => $v['sku_id'],
                    'goods_id' => $v['goods_id'],
                    'title' => $v['title'],
                    'sku_desc' => $v['sku_desc'],
                    'cover_img' => $v['cover_img_thumb_url']
                ];
            });
        }
        $data = [
            'all_count' => $list['aggregations'],
            'count' => $list['total'],
            'list' => $orderRefunds,
        ];
        return $data;
    }
}
